package com.alticor.amwaychina.contentHub

import android.Manifest
import android.os.Bundle
import android.os.Environment
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import com.alticor.amwaychina.weibolib.LaunchFFI
import com.sina.weibo.sdk.share.WbShareCallback
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
import android.Manifest.permission
import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
import android.support.v4.app.ActivityCompat
import android.content.pm.PackageManager
import android.support.v4.content.ContextCompat
import android.R.string
import android.content.Intent
import com.sina.weibo.sdk.share.WbShareHandler


class MainActivity : AppCompatActivity()  , WbShareCallback {
    private var shareHandler: WbShareHandler? = null
    override fun onWbShareFail() {
        Toast.makeText(this, "fail", Toast.LENGTH_LONG).show()
    }

    override fun onWbShareCancel() {
        Toast.makeText(this, "cancel", Toast.LENGTH_LONG).show()
    }

    override fun onWbShareSuccess() {
        Toast.makeText(this, "success", Toast.LENGTH_LONG).show()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        FileCopy.copyFile(this , "aaa.png")
        FileCopy.copyFile(this , "eeee.mp4")
        LaunchFFI.install(this)
        shareHandler = WbShareHandler(this)
        shareHandler?.registerApp()
        wb_share_text.setOnClickListener {
            LaunchFFI.shareText(this , "","美丽中","https://www.baidu.com")
        }
        wb_share_weblink.setOnClickListener {
            //KonyMain
            LaunchFFI.shareWebPage(this,"" ,"" ,0 ,
               "https://www.baidu.com","" )
        }
        wb_share_image.setOnClickListener {
            val picFile = File(getExternalFilesDir(null)!!.toString() + "/aaa.png")
            val path = Environment.getExternalStorageDirectory().toString()+"/aaa.png"
            LaunchFFI.shareLocalImage(this , "1","erere","https://www.baidu.com",path)
        }

        wb_share_video.setOnClickListener {
            val picFile = File(getExternalFilesDir(null)!!.toString() + "/eeee.mp4")
            val path = Environment.getExternalStorageDirectory().toString()+"/1/eeee.mp4"
            LaunchFFI.shareVideo(this , "1","erere","https://www.baidu.com",path)
        }
        qq_share_weblink.setOnClickListener {
            com.alticor.amwaychina.qqsharelib.LaunchFFI.shareWebPage(this ,"title","https://www.baidu.com","summary" ,"contentApp");
        }

        qq_share_image.setOnClickListener {
            val picFile = File(getExternalFilesDir(null)!!.toString() + "/aaa.png")
            //com.alticor.amwaychina.qqsharelib.LaunchFFI.shareLocalImage(this , "1","erere","https://www.baidu.com",picFile.toString())
        }
        //permission();

    }

//    fun permission(){
//        //使用兼容库就无需判断系统版本
//        val hasWriteStoragePermission =
//            ContextCompat.checkSelfPermission(application, Manifest.permission.WRITE_EXTERNAL_STORAGE)
//        if (hasWriteStoragePermission == PackageManager.PERMISSION_GRANTED) {
//            //拥有权限，执行操作
//
//        } else {
//            //没有权限，向用户请求权限
//            ActivityCompat.requestPermissions(
//                this,
//                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
//                1001
//            )
//        }
//    }

//    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
//        //通过requestCode来识别是否同一个请求
//        if (requestCode == 1001) {
//            if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                //用户同意，执行操作
//            } else {
//                //用户不同意，向用户展示该权限作用
//                if (ActivityCompat.shouldShowRequestPermissionRationale(
//                        this,
//                        Manifest.permission.WRITE_EXTERNAL_STORAGE
//                    )
//                ) {
//
//                }
//            }
//        }
//    }
//    private fun copyFile(fileName: String) {
//
//        val file = File(getExternalFilesDir(null)!!.path + "/" + fileName)
//        if (!file.exists()) {
//            //复制文件
//            val thread = Thread(Runnable {
//                try {
//                    val inputStream = assets.open(fileName)
//                    val outputStream = FileOutputStream(file)
//                    val buffer = ByteArray(1444)
//                    var readSize = 0
//                    while ((readSize = inputStream.read(buffer)) != 0) {
//                        outputStream.write(buffer, 0, readSize)
//                    }
//                    inputStream.close()
//                    outputStream.close()
//                } catch (e: Exception) {
//                }
//            })
//            thread.start()
//        }
//    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        shareHandler?.doResultIntent(data, this)
    }
}
